package www.worq.controller.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.worq.controller.beans.reciboBean;
import www.worq.model.dao.faqDao;
import www.worq.model.dao.oficinasDao;
import www.worq.model.dao.recibosDao;
import www.worq.model.dao.tramitesDao;

/**
 * Servlet implementation class worqControl
 */
@WebServlet(description = "controla el flujo de los jsp", urlPatterns = { "/worqControl" })
public class worqControlSVR extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String mapping = null;
	
	tramitesDao traDao = new tramitesDao();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public worqControlSVR() {
		super();
		System.out.println("SVR.Home----------> Ejecutandose!");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("SVR.home----------> doGet");

		try {
			mapping = "/VIEW/COMMONS/index.jsp";
		} catch (Exception error) {
			System.out
					.println("SVR.home.doGet.ErrorCargandoModuloInformativo----------> "
							+ error.getMessage());
			request.setAttribute("mensaje", error.getMessage());
		} finally {
			request.getRequestDispatcher(mapping).forward(request, response);
			System.out.println("SVR.home.doGet----------> forward.index.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("SVR.home----------> doPost");
		int dispatch = Integer.parseInt(request.getParameter("dispatch"));
		System.out.println(dispatch);

		try {
			System.out.println("SVR.home.doPost----------> infoDAO Cargado!");
			switch (dispatch) {
			case 1:
				// seleccionando el siguiente jsp
				mapping = "/VIEW/COMMONS/index.jsp";
				System.out
						.println("SVR.home.doPost----------> forward.index.jsp");
				break;
			case 2:
				// seleccionando el siguiente jsp
				mapping = "/VIEW/COMMONS/nosotros.jsp";
				System.out
						.println("SVR.home.doPost----------> forward.nosotros.jsp");
				break;
			case 3:
				// seleccionando el siguiente jsp
				mapping = "/VIEW/COMMONS/tramites.jsp";				
				request.setAttribute("cat_tramites",traDao.getTRAMITES());
				System.out
						.println("SVR.home.doPost----------> forward.tramites.jsp");
				break;
			case 8:
				//detalles de tramite
				mapping = "/VIEW/COMMONS/detalles.jsp";
				request.setAttribute("cat_tramites",traDao.getTRAMITE(Integer.parseInt(request.getParameter("id_tramite"))));
				System.out
						.println("SVR.home.doPost----------> forward.tramites.jsp");
				break;
			case 4:
				// seleccionando el siguiente jsp
				mapping = "/VIEW/COMMONS/ayuda.jsp";
				faqDao fdao = new faqDao();
				request.setAttribute("faqs",fdao.getFAQS());
				System.out
						.println("SVR.home.doPost----------> forward.ayuda.jsp");
				break;
			case 5:
				// seleccionando el siguiente jsp
				mapping = "/VIEW/COMMONS/busqueda.jsp";
				System.out
						.println("SVR.home.doPost----------> forward.busqueda.jsp");
				break;
			case 9:
				reciboBean reBean = new reciboBean();
				reBean.setId_recibo(Integer.parseInt(request.getParameter("folio")));
				recibosDao reDao = new recibosDao();
				reDao.inmprimeNota(reBean);
				
				if(reBean.getStatus().equals("null")){
					// seleccionando el siguiente jsp					
					mapping = "/VIEW/COMMONS/buesqueda.jsp";
				}else{
					request.setAttribute("recibo",reBean);
					// seleccionando el siguiente jsp
					mapping = "/VIEW/COMMONS/recibo.jsp";
				}
				
				System.out
						.println("SVR.home.doPost----------> forward.busqueda.jsp");
				break;
			case 6:
				// seleccionando el siguiente jsp
				mapping = "/VIEW/COMMONS/contacto.jsp";
				oficinasDao ofiDao = new oficinasDao();
				request.setAttribute("cat_oficinas",ofiDao.getOFICINAS());
				request.setAttribute("encargados",ofiDao.getENCARGADOS());
				System.out
						.println("SVR.home.doPost----------> forward.contacto.jsp");
				break;
			case 7:
				// seleccionando el siguiente jsp
				mapping = "/VIEW/SEGURIDAD/login.jsp";
				System.out
						.println("SVR.home.doPost----------> forward.login.jsp");
				break;
			}
		} catch (Exception error) {
			System.out
					.println("SVR.home.doPost.ErrorCargandoModuloInformativo----------> "
							+ error.getMessage());
			request.setAttribute("mensaje", error.getMessage());
		} finally {
			request.getRequestDispatcher(mapping).forward(request, response);
		}
	}

}
